//
//  ChoosePhotoView.m
//  WeCommunity
//
//  Created by mac on 2017/6/19.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ChoosePhotoView.h"

@implementation ChoosePhotoView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = WHITE_COLOR;
        
        self.contentImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        self.contentImageView.contentMode = UIViewContentModeScaleAspectFill;
        self.contentImageView.clipsToBounds = YES;
        self.contentImageView.exclusiveTouch = YES;
        self.contentImageView.userInteractionEnabled = YES;
        [self addSubview:self.contentImageView];
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage)];
        [self.contentImageView addGestureRecognizer:tapGes];
        
        self.chooseBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) - 32, 0, 32, 32)];
        [self.chooseBtn setImage:[UIImage imageNamed:@"choosePhoto"] forState:UIControlStateNormal];
        [self.chooseBtn setTitleColor:WHITE_COLOR forState:UIControlStateSelected];
        self.chooseBtn.titleLabel.font = PFM_FONT(14);
        [self.chooseBtn addTarget:self action:@selector(processBtn:) forControlEvents:UIControlEventTouchUpInside];
        self.chooseBtn.exclusiveTouch = YES;
        self.chooseBtn.layer.cornerRadius = CGRectGetWidth(self.chooseBtn.bounds) / 2;
        self.chooseBtn.clipsToBounds = YES;
        [self addSubview:self.chooseBtn];
        
        self.coverView = [[UIView alloc] initWithFrame:self.bounds];
        self.coverView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.6];
        [self addSubview:self.coverView];
        self.coverView.hidden = YES;
        
        
    }
    return self;
}

-(void)showImage
{
    if (self.delegate && self.photoAlbumModel) {
        [self.delegate preViewPhotoAlbum:self.photoAlbumModel];
    }
    if (self.delegate && self.preViewPhotoAlbumModel) {
        [self.delegate preViewPhotoAlbum:self.preViewPhotoAlbumModel];
    }
}

-(void)processBtn:(UIButton *)sender
{
    if (self.delegate && self.photoAlbumModel) {
        [self.delegate choosePhotoAlbum:self.photoAlbumModel sender:sender];
    }
    if (self.delegate && self.preViewPhotoAlbumModel) {
        [self.delegate choosePhotoAlbum:self.preViewPhotoAlbumModel sender:sender];
    }
}

-(void)setPreViewPhotoAlbumModel:(PhotoAlbumModel *)preViewPhotoAlbumModel
{
    _preViewPhotoAlbumModel = preViewPhotoAlbumModel;

    if (_preViewPhotoAlbumModel.assetImage) {
        [self.contentImageView setImage:_preViewPhotoAlbumModel.assetImage];
    }else{
        @weakify(self);
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            UIImage * image = [UIImage imageWithCGImage:[_preViewPhotoAlbumModel.asset aspectRatioThumbnail]];
            dispatch_async(dispatch_get_main_queue(), ^{
                @strongify(self);
                _preViewPhotoAlbumModel.assetImage = image;
                [self.contentImageView setImage:image];
            });
        });
    }
    
}

-(void)setPhotoAlbumModel:(PhotoAlbumModel *)photoAlbumModel
{
    _photoAlbumModel = photoAlbumModel;
    
    if (_photoAlbumModel.assetImage) {
        [self.contentImageView setImage:_photoAlbumModel.assetImage];
    }else{
        @weakify(self);
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            UIImage * image = [UIImage imageWithCGImage:[_photoAlbumModel.asset aspectRatioThumbnail]];
            dispatch_async(dispatch_get_main_queue(), ^{
                @strongify(self);
                _photoAlbumModel.assetImage = image;
                [self.contentImageView setImage:image];
            });
        });
    }
    
    self.chooseBtn.selected = _photoAlbumModel.selected;
    if (_photoAlbumModel.selected) {
        self.chooseBtn.backgroundColor = TINT_GREEN_COLOR;
        [self.chooseBtn setImage:nil forState:UIControlStateNormal];
        [self.chooseBtn setImage:nil forState:UIControlStateSelected];
    }else{
        self.chooseBtn.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
        [self.chooseBtn setImage:[UIImage imageNamed:@"choosePhoto"] forState:UIControlStateNormal];
    }
  
    self.coverView.hidden = _photoAlbumModel.canSelected;
    self.contentImageView.userInteractionEnabled = _photoAlbumModel.canSelected;
    self.chooseBtn.userInteractionEnabled = _photoAlbumModel.canSelected;

    if (_photoAlbumModel.selectedIndex > 0) {
        [self.chooseBtn setTitle:[NSString stringWithFormat:@"%ld",_photoAlbumModel.selectedIndex] forState:UIControlStateSelected];
    }
}

@end
